package cn.com.frame.commom;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 数据分页查询
 * @param <T>
 */
public class PageBasic<T> {

    private int total;//总数
    private int pageCount;//共几页
    private Integer page;//第几页
    private Integer size;//每页显示数据
    private List<T> rows ;//集合数据

    public PageBasic(Integer page,Integer size){

        setPageSize(page, size);
    }
   // public PageBasic(){}

    /*** set,get ***/
    /**
     * 赋值每页数量和当前页
     * @param page
     * @param size
     */
    public void setPageSize(Integer page,Integer size){
        if(null==page || 0 >=page){
            page = 1;
        }
        if(null == size || 0>=size){
            size = PageConfigUtil.defaultSize;
        }
        if(null!=size && size > PageConfigUtil.topSize){
            size = PageConfigUtil.defaultSize;
        }
        this.page = page;
        this.size = size;
        /**
         * 分页插件
         */
        PageHelper.startPage(page,size);
    }

    public int getTotal() {
        return total;
    }

    /**
     * 赋值总数 ，并计算页数
     * @param total
     */
    public void setTotal(int total) {
        this.total = total;
        if (total > 0) {
            if (total > 0) {
                this.pageCount = total % size == 0 ? total / size : (total / size) + 1;
            }
        }
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public Integer getPage() {
        return page;
    }

    public Integer getSize() {
        return size;
    }


    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
        if(null == rows){
            this.rows = Collections.EMPTY_LIST;
        }

        if (rows instanceof Page) {
            Page page = (Page)rows;
            setTotal((int)page.getTotal());
        }else if(rows instanceof Collection){
            //Collection collection = (Collection) rows;
            setTotal(rows.size());
        }

    }

    /**
     * 内部分页处理数据调用传入每页数量(参数不需要用户输入调用)
     * @param page
     * @param size
     */
    /*public void setInsidePageSize(Integer page,Integer size){
        if(null==page || 0 >=page){
            page = 1;
        }
        if(null == size || 0>=size){
            size = PageConfigUtil.defaultSize;
        }
        if(null!=size && size > PageConfigUtil.topInsideSize){
            size = PageConfigUtil.topInsideSize;
        }
        this.page = page;
        this.size = size;
    }*/
}
